package vboot.core.module.ass.num.main;

import vboot.core.common.api.R;
import vboot.core.common.dao.Sqler;
import vboot.core.xutil.lang.XstrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("ass/num/main")
public class AssNumMainApi {

    @GetMapping
    public R get(String name) {
        Sqler sqler = new Sqler("t.*","ass_num_main");
        sqler.addLike("t.name",name);
        sqler.addSelect("t.nupre,t.nunex,t.numod");
        return R.ok(service.findPageData(sqler)) ;
    }

    @GetMapping("one/{id}")
    public R getOne(@PathVariable String id) {
        AssNumMain main=repo.findById(id).get();
        return R.ok(main);
    }

    @PostMapping
    public synchronized R post(@RequestBody AssNumMain main) {
        if(repo.existsById(main.getId())){
            return R.build(201,"编号已存在，请修改编号");
        }
        main.setNflag(true);
        repo.save(main);
        return R.ok();
    }

    @PutMapping
    public R pust(@RequestBody AssNumMain main) {
        if(XstrUtil.isBlank(main.getNunex())){
            main.setNflag(true);
        }
        main.setUptim(new Date());
        repo.save(main);
        return R.ok();
    }

    @DeleteMapping("{ids}")
    public R delete(@PathVariable String[] ids) {
        for (String id : ids) {
            repo.deleteById(id);
        }
        return R.ok();
    }

    @Autowired
    private AssNumMainRepo repo;

    @Autowired
    private AssNumMainService service;
}
